<?php
namespace UploadFiles;
class UploadFiles
{
	private $uploadFolder = 'plugin/UploadFiles/upload/';
	private $pagePath;

	public function __construct()
	{
		$pagePath = '?' . $_SERVER['QUERY_STRING'];
		$buildPagePath = explode( "&", $pagePath );
		$this->pagePath = $buildPagePath[0];
	}

	public function RunPlugin()
	{
		require_once 'UploadFilesModel.php';
		require_once 'UploadFilesView.php';

		$uploadFilesView = new UploadFilesView();
		$uploadFilesModel = new UploadFilesModel();

		$html = '';

		if ( $uploadFilesView->TriedToUploadFile() )
		{
			if ( $uploadFilesModel->UploadFile() )
			{
				$html .= 'Filen ('. $_FILES['file']['name'] . ') laddades upp utan problem.';
			}
			else
			{
				$html .= 'Någonting gick fel';
			}

			if ( $_FILES['file']['error'] > 0 )
			{
				$html .= $_FILES['file']['error'];
			}
		}

		$html .= $uploadFilesView->DoUploadFilesForm();

		if ( isset( $_GET['showDirList'] ) && $_GET['showDirList'] == 1 )
		{
			$html .= $uploadFilesView->ListDirectory( $uploadFilesModel->ScanDirectory( "plugin/UploadFiles/upload" ), $this->pagePath );
		}

		// Directory navigation.
		if ( isset ( $_GET['dir'] ) )
		{
			$html .= $uploadFilesView->ListDirectoryContent( $uploadFilesModel->ScanDirectory( $this->uploadFolder . $_GET['dir'] ), $this->pagePath );
		}
		else if ( ! isset ( $_GET['showDirList'] ) )
		{
			$html .= $uploadFilesView->ListDirectory( $uploadFilesModel->ScanDirectory( "plugin/UploadFiles/upload" ), $this->pagePath );
		}

		return $html;
	}
}